#include "table.h"
#include "ui_table.h"

table::table(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::table)
{
    ui->setupUi(this);
    ui->from->setColumnCount(16);
    ui->from->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // 设置窗体最大化和最小化
    Qt::WindowFlags windowFlag  = Qt::Dialog;
    windowFlag                  |= Qt::WindowMinimizeButtonHint;
    windowFlag                  |= Qt::WindowMaximizeButtonHint;
    windowFlag                  |= Qt::WindowCloseButtonHint;

    setWindowFlags(windowFlag);
}

table::~table()
{
    delete ui;
}

void table::setTable(QString content){
    row = 1;
    ui->from->clear();
    ui->from->setRowCount(1);
    QStringList strList = content.split(" ");
    int i=0,j=0,a=0;
    while(a<strList.length()){
        QTableWidgetItem *item = new QTableWidgetItem(strList[a]);
        if(strList[a]!="00"){
            item->setBackgroundColor(QColor(225, 225, 225));
        }
        a++;
        ui->from->setItem(i,j++,item);
        if(j==16){
            j=0;
            i++;
            row++;
            ui->from->insertRow(i);
            QStringList Vlist;
            QList<int> list;
            int b=0;
            for(int k=0;k<=row;k++){
                list<<b;
                b+=16;
            }
            //设置垂直表头
            for(auto value : list){
                QString str =  QString("%1").arg(value,4,16,QLatin1Char('0'));
                Vlist<<str;
            }
            ui->from->setVerticalHeaderLabels(Vlist);
        }
    }
    QStringList Hlist;
    Hlist<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"A"<<"B"<<"C"<<"D"<<"E"<<"F";
//            设置水平表头
    ui->from->setHorizontalHeaderLabels(Hlist);

    for(int t = 0; t<16; t++){
        for(int j = 0; j<(row-1); j++){
            ui->from->item(j, t)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
}
